package model;

import datenbank.ModelAllgemein;
import datenbank.ModelStat1;
import datenbank.ModelStat10;
import datenbank.ModelStat11;
import datenbank.ModelStat12;
import datenbank.ModelStat2;
import datenbank.ModelStat3;
import datenbank.ModelStat4;
import datenbank.ModelStat5;
import datenbank.ModelStat6;
import datenbank.ModelStat7;
import datenbank.ModelStat8;
import datenbank.ModelStat9;

import java.util.BitSet;
import java.util.Observable;

public class TCPModelRoboter extends Observable {
	private volatile BitSet ioSenden = new BitSet(64);

	private volatile float[] floatSendenParamater = new float[256];

	private volatile BitSet ioEmpf = new BitSet(64);

	private volatile float[] floatEmpfParamater = new float[128];

	private ModelAllgemein modelAllgemein;
	private ModelStat1 modelStat1;
	private ModelStat2 modelStat2;
	private ModelStat3 modelStat3;
	private ModelStat4 modelStat4;
	private ModelStat5 modelStat5;
	private ModelStat6 modelStat6;
	private ModelStat7 modelStat7;
	private ModelStat8 modelStat8;
	private ModelStat9 modelStat9;
	private ModelStat10 modelStat10;
	private ModelStat11 modelStat11;
	private ModelStat12 modelStat12;

	/**
	 * Konstruktor
	 * 
	 * @param modelAllgemein
	 * @param modelStat1
	 * @param modelStat2
	 * @param modelStat3
	 * @param modelStat4
	 * @param modelStat5
	 * @param modelStat6
	 * @param modelStat7
	 * @param modelStat8
	 * @param modelStat12
	 * @param modelStat11
	 * @param modelStat10
	 */
	public TCPModelRoboter(ModelAllgemein modelAllgemein, ModelStat1 modelStat1, ModelStat2 modelStat2, ModelStat3 modelStat3,
			ModelStat4 modelStat4, ModelStat5 modelStat5, ModelStat6 modelStat6, ModelStat7 modelStat7, ModelStat8 modelStat8,
			ModelStat9 modelStat9, ModelStat10 modelStat10, ModelStat11 modelStat11, ModelStat12 modelStat12) {

		this.modelAllgemein = modelAllgemein;

		this.modelStat1 = modelStat1;
		this.modelStat2 = modelStat2;
		this.modelStat3 = modelStat3;
		this.modelStat4 = modelStat4;
		this.modelStat5 = modelStat5;
		this.modelStat6 = modelStat6;
		this.modelStat7 = modelStat7;
		this.modelStat8 = modelStat8;
		this.modelStat9 = modelStat9;
		this.modelStat10 = modelStat10;
		this.modelStat11 = modelStat11;
		this.modelStat12 = modelStat12;
	}

	/**
	 * 
	 */
	private void datenAuslFloat() {

		int j = 0;

		// Station 1
		int k = 43;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat1.getDBEintragFloat(k);

			k += 2;
		}

		// Station 2
		k = 43;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat2.getDBEintragFloat(k);

			k += 2;
		}

		// Station 3
		k = 43;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] =modelStat3.getDBEintragFloat(k);

			k += 2;
		}

		// Station 4
		k = 43;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat4.getDBEintragFloat(k);

			k += 2;
		}

		// Station 5
		k = 43;
		for (int i = 0; i < 20; i++) {
			floatSendenParamater[j++] = modelStat5.getDBEintragFloat(k);

			k += 2;
		}

		// Station 6
		k = 43;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat6.getDBEintragFloat(k);

			k += 2;
		}

		// Station 7
		k = 43;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat7.getDBEintragFloat(k);

			k += 2;
		}

		// Station 8
		k = 43;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat8.getDBEintragFloat(k);

			k += 2;
		}

		// Station 9
		k = 43;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat9.getDBEintragFloat(k);

			k += 2;
		}

		// Station 10
		k = 43;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat10.getDBEintragFloat(k);

			k += 2;
		}

		// Station 11
		k = 43;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat11.getDBEintragFloat(k);

			k += 2;
		}

		// Station 12
		k = 43;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat12.getDBEintragFloat(k);

			k += 2;
		}

		k = 2;

		j = 200;

		for (int i = 0; i < 30; i++) {
			floatSendenParamater[j++] = modelAllgemein.getDBEintragFloat(k++);
		}

		floatSendenParamater[230] = modelStat1.getDBEintragFloat(1);
	}

	/**
	 * 
	 */
	private void artikelKopiert() {
		if ((getFloatParameter(2) != 0.0F) && (getFloatParameter(2) == floatSendenParamater[230]))
			setFloatParameter(232, 0);
	}

	/**
	 * 
	 * @param daten
	 */
	public void empfangen(String[] daten) {
		byte[] b = new byte[8];

		int j = 0;

		for (int i = 0; i < b.length; i++) {
			b[i] = Byte.valueOf(daten[j++]);
		}

		ioEmpf = BitSet.valueOf(b);

		for (int i = 0; i < floatEmpfParamater.length; i++) {
			floatEmpfParamater[i] = Float.valueOf(daten[j++]);
		}

		artikelKopiert();
	}

	/**
	 * 
	 * @return Sende String
	 */
	public String datenSenden() {
		byte[] b_copy = new byte[1];
		byte[] b = new byte[ioSenden.size() / 8];

		b_copy = ioSenden.toByteArray();

		for (int i = 0; i < b.length; i++) {
			if (i < b_copy.length)
				b[i] = b_copy[i];
			else {
				b[i] = 0;
			}

		}

		String[] io = new String[b.length];

		for (int i = 0; i < io.length; i++) {
			io[i] = String.valueOf(b[i]);
		}

		StringBuilder sb = new StringBuilder();

		sb.append("S");

		for (int i = 0; i < io.length; i++) {
			sb.append(io[i] + ";");
		}

		datenAuslFloat();

		for (int i = 0; i < floatSendenParamater.length; i++) {
			sb.append(floatSendenParamater[i] + ";");
		}

		sb.append("E");

		return sb.toString();
	}

	/**
	 * 
	 * @param index
	 * @param wert
	 */
	public void setIO(int index, boolean wert) {
		if (wert)
			ioSenden.set(index);
		else
			ioSenden.clear(index);
	}

	/**
	 * 
	 * @param index
	 * @return IO
	 */
	public boolean getIO(int index) {
		if (index < ioEmpf.size()) {
			return ioEmpf.get(index);
		}

		return false;
	}

	/**
	 * 
	 * @param index
	 * @return Parameter
	 */
	public float getFloatParameter(int index) {
		return floatEmpfParamater[index];
	}

	/**
	 * 
	 * @param index
	 * @param wert
	 */
	public void setFloatParameter(int index, int wert) {
		floatSendenParamater[index] = wert;
	}

	/**
	 * 
	 * @param copy
	 */
	public void copyArtNr(String copy) {
		setFloatParameter(102, Integer.parseInt(copy));
	}
}